package com.robot.controller;

import com.robot.model.User;
import com.robot.service.UserService;
import com.robot.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/6/23
 * Time: 10:04
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/login")
@Slf4j
//@SessionAttributes
public class LoginController {

    @Autowired
    UserService userService;

    @RequestMapping("/loginPage")
    public String loginPage(){
        return "/login/login";
    }
    @RequestMapping("/registerPage")
    public String registerPage(){
        return "/login/register";
    }
    @RequestMapping("/login")
    public ModelAndView login(@Valid User userDo, BindingResult bindingResult, HttpSession httpSession){
        ModelAndView modelAndView=new ModelAndView();
        if(bindingResult.hasErrors()){
            String msg =bindingResult.getFieldError().getDefaultMessage();
            modelAndView.addObject("msg",msg);
            modelAndView.addObject("user",userDo);
            modelAndView.setViewName("/login/loginPage");
            return modelAndView;
        }
        ResponseMessage responseMessage= userService.checkUser(userDo);
        if(!responseMessage.isSuccess()){
            modelAndView.addObject("msg",responseMessage.getMsg());
            modelAndView.addObject("user",userDo);
            modelAndView.setViewName("/login/loginPage");
            //不是真正意义上的方法，调用控制层，调用的registerPage,的路径
            return modelAndView;
        }
        httpSession.setAttribute("user",responseMessage.getData());
        modelAndView.setViewName("redirect:/index");
        return modelAndView;
    }
    @RequestMapping("/register")
    public ModelAndView register(@Valid User userDo, BindingResult bindingResult){
        ModelAndView modelAndView=new ModelAndView();
        if(bindingResult.hasErrors()){
            String msg =bindingResult.getFieldError().getDefaultMessage();
            modelAndView.addObject("msg",msg);
            modelAndView.addObject("user",userDo);
            modelAndView.setViewName("/login/registerPage");
            return modelAndView;
        }
        ResponseMessage responseMessage = userService.registerUser(userDo);
        if(!responseMessage.isSuccess()){
            modelAndView.addObject("msg",responseMessage.getMsg());
            modelAndView.setViewName("/login/registerPage");
            //不是真正意义上的方法，调用控制层，调用的registerPage,的路径
            return modelAndView;
        }
        modelAndView.setViewName("redirect:/login/loginPage");
        return modelAndView;
    }
    @RequestMapping("/loginout")
    public String loginOut(HttpSession httpSession){
        httpSession.invalidate();
        return "redirect:/login/loginPage";
    }
}
